package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2017/1/3 0003.
 */
@ContentView(R.layout.new_activity_user_info)
public class UserInfoActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.iv_user_photo)
    ImageView mIvUserPhoto;
    @ViewInject(R.id.tv_nick_name)
    TextView mTvNickName;
    @ViewInject(R.id.tv_adress)
    TextView mTvAdress;
    @ViewInject(R.id.tv_sex)
    TextView mTvSex;
    @ViewInject(R.id.tv_both)
    TextView mTvBoth;
    @ViewInject(R.id.tv_autograph)
    TextView tv_autograph;
    @ViewInject(R.id.ll_autograph)
    LinearLayout mLlAutograph;
    @ViewInject(R.id.ll_nick)
    LinearLayout mLLNick;
    private String getUserInfoUrl = Common.mUrl + "szdh/userInfo.php?type=query&uid=";
    public static boolean isName = true;
    public static String nameTExt = "";
    public static String Title = "更改名字";
    public static String photeUrl = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mTvTitle.setText("编辑资料");
        mIvBack.setOnClickListener(this);
        mLlAutograph.setOnClickListener(this);
        mLLNick.setOnClickListener(this);
        mIvUserPhoto.setOnClickListener(this);
        RequestHttp.getHttp(this, getUserInfoUrl + Common.mUserId, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                initData(jsonObject);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_nick:
                isName = true;
                Title = "更改名字";
                nameTExt = mTvNickName.getText().toString();
                startActivityForResult(new Intent(UserInfoActivity.this, UpNameActivity.class), 0);
                break;
            case R.id.ll_autograph:
                isName = false;
                Title = "个性签名";
                nameTExt = tv_autograph.getText().toString();
                startActivityForResult(new Intent(UserInfoActivity.this, UpNameActivity.class), 0);
                break;
            case R.id.iv_user_photo:
                startActivityForResult(new Intent(UserInfoActivity.this, UpdateUserPhotoActivity.class), 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 0) {
            if (isName) {
                mTvNickName.setText(nameTExt);
            } else {
                tv_autograph.setText(nameTExt);
            }
        } else {
            RequestHttp.downToIamge(this, photeUrl, mIvUserPhoto);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initData(JSONObject jsonObject) {
        try {
            if (jsonObject.getBoolean("state")) {
                JSONObject object = jsonObject.getJSONArray("data").getJSONObject(0);
                photeUrl = object.getString("headImg");
                RequestHttp.downToIamge(this, photeUrl, mIvUserPhoto);
                mTvNickName.setText(object.getString("nikeName"));
                mTvBoth.setText(object.getString("birthday"));
                mTvAdress.setText(object.getString("address"));
                mTvSex.setText(object.getString("sex"));
                tv_autograph.setText(object.getString("autograph"));
            } else {
                mTvNickName.setText("暂无昵称");
                mTvBoth.setText("2017年1月5日");
                mTvAdress.setText("绵阳");
                mTvSex.setText("男");
                tv_autograph.setText("快快编辑自己的个性签名吧!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
